/*******************************************************************************
 * FilePath    : /Software/Examples_Ble_MS1642/ble_app_peripheral_uart_led/Src/User/User_WeChat.c
 * Version     : 1.0
 * Brief       :
 * Author      : AndrewHu
 * Company     : Shanghai MacroGiga Electronics CO.,Ltd
 * Email       : Hubinbo@macrogiga.com
 * Date        : 2020-04-03 15:15:31
 * LastEditors : AndrewHu
 * LastEditTime: 2023-11-16 17:58:09
 * Description :
 ******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include <Includes.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define Test_Printf
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 ledOnOff = 0x00;     // 0x00-关
u8 ledTemp[5] = {0x00}; // r\g\b\c\w
u8 ledBuff[5] = {0x00}; // r\g\b\c\w
/* Private function ----------------------------------------------------------*/
/* extern --------------------------------------------------------------------*/
void Fun_WeChart_Led_onOff(u8 *arry, u16 len)
{
    if (arry[0] == 0x00)
    { // off
        ledOnOff = 0x00;
        AHL_Data_ArryInit(ledTemp, 0, 5, 0x00); // 清空数组
        Fun_PWM_Led_set(ledTemp);

        // test
        Printf_NewLine();
        Uart_Send_String((u8 *)"Led:off", 7);
    }
    else
    { // on
        ledOnOff = 0x01;
        if (AHL_Data_BlankCheck(ledBuff, 5) == TRUE)
        { // 数据为空
            ledBuff[0] = 0x00;
            ledBuff[1] = 0x00;
            ledBuff[2] = 0x00;
            ledBuff[3] = 0x80;
            ledBuff[4] = 0x80;
        }

        Fun_PWM_Led_set(ledBuff);

#ifdef Test_Printf // test
        Printf_NewLine();
        Uart_Send_String((u8 *)"Led:on", 6);
        Printf_NewLine();
        Uart_Send_String((u8 *)"Red:", 4);
        Uart_Send_Byte2Ascii(ledBuff[0]);
        Printf_NewLine();
        Uart_Send_String((u8 *)"Green:", 6);
        Uart_Send_Byte2Ascii(ledBuff[1]);
        Printf_NewLine();
        Uart_Send_String((u8 *)"Blue:", 5);
        Uart_Send_Byte2Ascii(ledBuff[2]);
        Printf_NewLine();
        Uart_Send_String((u8 *)"Cold:", 5);
        Uart_Send_Byte2Ascii(ledBuff[3]);
        Printf_NewLine();
        Uart_Send_String((u8 *)"Warm:", 5);
        Uart_Send_Byte2Ascii(ledBuff[4]);
#endif
    }
}
void Fun_WeChart_Led(u8 *arry, u16 len)
{
    u32 temp = 0x00;
    if (len != 11)
        return;

    if (arry[0] & 0x02)
    { // c
        temp = arry[2];
        temp <<= 8;
        temp += arry[1];

        temp <<= 8;
        temp = temp / 1000;
        ledBuff[3] = temp;
    }
    else
        ledBuff[3] = 0x00;

    if (arry[0] & 0x04)
    { // g
        temp = arry[4];
        temp <<= 8;
        temp += arry[3];

        temp <<= 8;
        temp = temp / 1000;
        ledBuff[1] = temp;
    }
    else
        ledBuff[1] = 0x00;

    if (arry[0] & 0x08)
    { // w
        temp = arry[6];
        temp <<= 8;
        temp += arry[5];

        temp <<= 8;
        temp = temp / 1000;
        ledBuff[4] = temp;
    }
    else
        ledBuff[4] = 0x00;

    if (arry[0] & 0x10)
    { // b
        temp = arry[8];
        temp <<= 8;
        temp += arry[7];

        temp <<= 8;
        temp = temp / 1000;
        ledBuff[2] = temp;
    }
    else
        ledBuff[2] = 0x00;

    if (arry[0] & 0x20)
    { // r
        temp = arry[10];
        temp <<= 8;
        temp += arry[9];

        temp <<= 8;
        temp = temp / 1000;
        ledBuff[0] = temp;
    }
    else
        ledBuff[0] = 0x00;

    Fun_PWM_Led_set(ledBuff);

#ifdef Test_Printf // test
    Printf_NewLine();
    Uart_Send_String((u8 *)"Led:on", 6);
    Printf_NewLine();
    Uart_Send_String((u8 *)"Red:", 4);
    Uart_Send_Byte2Ascii(ledBuff[0]);
    Printf_NewLine();
    Uart_Send_String((u8 *)"Green:", 6);
    Uart_Send_Byte2Ascii(ledBuff[1]);
    Printf_NewLine();
    Uart_Send_String((u8 *)"Blue:", 5);
    Uart_Send_Byte2Ascii(ledBuff[2]);
    Printf_NewLine();
    Uart_Send_String((u8 *)"Cold:", 5);
    Uart_Send_Byte2Ascii(ledBuff[3]);
    Printf_NewLine();
    Uart_Send_String((u8 *)"Warm:", 5);
    Uart_Send_Byte2Ascii(ledBuff[4]);
#endif
}
/*******************************************************************************
 * Function    : Fun_WeChat_Data
 * Brief       : 小程序数据处理
 * Parameter   :
 * Returns     :
 * Date        : 2023-11-17 16:47:55
 * Description : 小程序数据处理
 ******************************************************************************/
void Fun_WeChat_Data(u8 *arry, u16 len)
{
#ifdef Test_Printf // test
    Printf_NewLine();
    Uart_Send_Array2Ascii(arry, len, 1); // test
#endif

    switch (arry[0])
    {
    case 0xaa: // 开关设置,0x01-开，0x00-关。
        Fun_WeChart_Led_onOff(&arry[1], len - 1);
        break;
    case 0xab: // 灯色、亮度设置
        Fun_WeChart_Led(&arry[1], len - 1);
        break;
    case 0xac: // 遥控车控制

        break;
    case 0xad: // 跳绳计数

        break;
    case 0xae: // 温度显示

        break;
    default:
        break;
    }
}
